
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>


void data_deal(int fd)
{
	int data[2];
    int ret;
    
    ret = read(fd, data, 8);
    if(ret == 8)
    {
        printf("get humidity: %.2f %RH\n", data[0]/100.0);
        printf("get temperature: %.2f ℃\n", data[1]/100.0);
    }
    else {
        printf("Result error code : %d\n", ret);
    }
}


/*
 * ./sr04_test /dev/xxhsr04
 *
 */
int main(int argc, char **argv)
{
    int flags;
    int fd;
    int val;
    int ret;
    
    /* 1. 判断参数 */
    if (argc != 2) 
    {
        printf("Usage: %s <dev>\n", argv[0]);
        return -1;
    }

    /* 2. 打开文件 */
    fd = open(argv[1], O_RDWR);
    if (fd == -1)
    {
        printf("can not open file %s\n", argv[1]);
        return -1;
    }

    /* 启动信号机制 */
    //signal(SIGIO, sr501_signal_callback);
    //fcntl(fd, F_SETOWN, getpid());
    // flags = fcntl(fd, F_GETFL);
    // fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);

    while (1)
    {
        data_deal(fd);
        sleep(2);
    }
    
    close(fd);
    
    return 0;
}


